import Utils, { GraphicsHelper } from "../../core/utils";
import ShaderOpacityMask from "../../shaders/opacity_mask/shader_opacity_mask";
import Character from "./assets/game/character";
import World from "./world";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BulletLight extends cc.Component {
    @property(World)
    world: World = null;

    @property(cc.Prefab)
    light: cc.Prefab = null;

    @property(cc.Node)
    lightContain: cc.Node = null;

    darkMapSprite: cc.Sprite = null;

    @property(cc.Camera)
    darkCam: cc.Camera = null;

    rt: cc.RenderTexture = null;

    @property(ShaderOpacityMask)
    omh: ShaderOpacityMask = null;

    protected onLoad(): void {
        this.darkMapSprite = this.addComponent(cc.Sprite);
        this.darkMapSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
        this.node.on(cc.Node.EventType.SIZE_CHANGED, this.onSizeChanged, this);
        this.reset();
    }

    onSizeChanged() { 
        this.reset();
    }

    reset() {
        this.rt = new cc.RenderTexture();
        this.rt.initWithSize(this.node.width, this.node.height, cc.RenderTexture.DepthStencilFormat.RB_FMT_D24S8);
        this.darkMapSprite.spriteFrame = new cc.SpriteFrame(this.rt);
        this.darkCam.targetTexture = this.rt;
        this.darkCam.enabled = false;
        this.darkCam.alignWithScreen = true;
        this.darkCam.zoomRatio = Utils.game.node.height / this.node.height;

        this.omh.maskTex = this.rt;
        this.omh.node.zIndex = 9999;
    }


    protected lateUpdate(dt: number): void {
        this.darkCam.render(this.lightContain);
    }
};
